﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer ;
using ECI.CommonLibrary;
using ECI.Controls;
using ECI.CoreLibrary;
using ECI.WebLib.Data;

namespace ECI.DynamicData
{
    public  class DynamicData
    {
        private static ECI.CoreLibrary.CommonLibrary cmn = new ECI.CoreLibrary.CommonLibrary();
 
        public static DataTable GetDynamicData(string sql)
        {
            return DBHelper.GetDataTable(sql);
        }

        public static DataTable GetDynamicData(string sql, ECI.CommonLibrary.ECIPageInfo pageInfo)
        {
            pageInfo.PageIndex = 1;
            pageInfo.PageSize = 10;
            pageInfo.RecordCount = 100;
            pageInfo.PageCount = 10;
            return DBHelper.GetDataTable(sql);
        }

        public static  void DynamicInit(DynamicModel dynamicModel)
        {
            string sql = "";
            sql = " SELECT A.GUID,A.SQL FROM ECI_DYNAMIC_DATA A WHERE A.PAGE_ID=" + cmn.SQLQ(dynamicModel.PageID.ToUpper());
            sql += " AND A.GRIDVIEW_ID=" + cmn.SQLQ(dynamicModel.GridViewID.ToUpper());
            DataTable data= DBHelper.GetDataTable(sql);
            if(cmn.CheckEOF(data))
            {
                string regGuid = data.Rows[0]["GUID"].ToString();
                string regSql = data.Rows[0]["SQL"].ToString();
                sql = " SELECT B.DIV_HEIGHT,B.DIV_WIDTH,B.GRIDVIEW_HEIGHT,PAGE_SIZE";
                sql  +=" ,B.GRIDVIEW_WIDTH,B.GUID AS MAIN_GUID ";
                sql += " FROM ECI_DYNAMIC_MAIN B";
                sql += " WHERE B.REG_GUID=" + cmn.SQLQ(regGuid);
                string mysql = sql + " AND B.COMPANY_ID=" + cmn.SQLQ(dynamicModel.CustomerID);

                dynamicModel.DynamicMain =DBHelper.GetDataTable(sql);

                if (cmn.CheckEOF(dynamicModel.DynamicMain))
                {
                    sql += " AND B.COMPANY_ID IS NULL";
                    dynamicModel.DynamicMain = DBHelper.GetDataTable(sql);
                }

                if (!cmn.CheckEOF(dynamicModel.DynamicMain))
                {
                    return ;
                }

                string mainGuid = dynamicModel.DynamicMain.Rows[0]["MAIN_GUID"].ToString();
                sql = " SELECT DB_FIELD,FIELD_NAME,CH_NAME,FIELDWIDTH,FORMART,IS_SORT,SORT_SEQ_NO,";
                sql += " FIELD_TYPE,FIELD_PARMETER";
                sql += " ECI_DYNAMIC_DETAIL ";
                sql += " WHERE MAIN_GUID=" + cmn.SQLQ(mainGuid);
                sql += " AND IS_SHOW='Y'";
                sql += " ORDER BY SEQ_NO";

                dynamicModel.DynamicDetail = DBHelper.GetDataTable(sql);
                if (!cmn.CheckEOF(dynamicModel.DynamicDetail))
                {
                    return ;
                }

                string column = "";
                foreach (DataRow row in dynamicModel.DynamicDetail.Rows)
                {
                    column =","+ row["DB_FIELD"].ToString() + " AS " + row["FIELD_NAME"].ToString();
                }
                if (column.Length > 0) column = column.Substring(1);

                sql = " SELECT WM_CONCAT(C.DB_FIELD) FROM ECI_DYNAMIC_DETAIL C";
                sql += " WHERE C.IS_SORT='Y' AND C.IS_SHOW='Y' AND C.MAIN_GUID=" + cmn.SQLQ(mainGuid);
                sql += " ORDER BY SORT_SEQ_NO ";
                DataTable dataSort =  DBHelper.GetDataTable(sql);
                if (cmn.CheckEOF(dataSort))
                {
                    dynamicModel.Sort = " ORDER BY " + dataSort.Rows[0][0].ToString();
                }

                dynamicModel.DynamicSql = "SELECT " + column + " FROM " + regSql;
            }
        }    

        public static void InitGirdColumn(GridView gridView, Panel pnlGrid, AspNetPager AspNetPager, DynamicModel dynamicModel)
        {
            gridView.EnableViewState = true;
            gridView.Columns.Clear();
            foreach (DataRow row in dynamicModel.DynamicDetail.Rows)
            {
                ButtonField buttonField;
                TemplateField templateField;
                string fieldType = row.GetFieldValue("FIELDTYPE", "BOUND");
                string fieldWidth = row.GetFieldValue("FIELDWIDTH");
                string headerText = row.GetFieldValue("CHNAME");
                string dataField = row.GetFieldValue("FIELDNAME");
                string formatString = row.GetFieldValue( "FORMAT");
                string fieldPara =row.GetFieldValue("FIELDPARA");
                string _FieldType1 = fieldType;
                if (_FieldType1 != null)
                {
                    if (!(_FieldType1 == "BOUND"))
                    {
                        if (_FieldType1 == "BUTTON")
                        {
                            buttonField = new ButtonField();
                            buttonField.HeaderText = headerText;
                            buttonField.DataTextField = dataField;
                            if (fieldWidth != "")
                            {
                                buttonField.ItemStyle.Width = new Unit(double.Parse(fieldWidth));
                            }
                            if (fieldPara != "")
                            {
                                buttonField.CommandName = fieldPara;
                            }
                            gridView.Columns.Add(buttonField);
                        }
                        if (_FieldType1 == "CHECKSEL")
                        {
                            templateField = new TemplateField();
                            templateField.ItemTemplate = new CustomGridViewTemplate(DataControlRowType.DataRow, "", dataField, "S");
                            string _headerTxt = "<input id='chkSelectAll' onClick='funSelectAll(this," + gridView.ID + ")' type='checkbox'>";
                            templateField.HeaderTemplate = new CustomGridViewTemplate(DataControlRowType.Header, _headerTxt, "", "S");
                            gridView.Columns.Add(templateField);
                        }
                        if (_FieldType1 == "CHECK")
                        {
                            templateField = new TemplateField();
                            templateField.ItemTemplate = new CustomGridViewTemplate(DataControlRowType.DataRow, "", dataField, "C");
                            templateField.HeaderTemplate = new CustomGridViewTemplate(DataControlRowType.Header, headerText, "", "C");
                            gridView.Columns.Add(templateField);
                        }
                        if (_FieldType1 == "HIDDEN")
                        {
                            templateField = new TemplateField();
                            templateField.ItemTemplate = new CustomGridViewTemplate(DataControlRowType.DataRow, "", dataField, "H");
                            templateField.HeaderTemplate = new CustomGridViewTemplate(DataControlRowType.Header, headerText, "", "H");
                            templateField.ItemStyle.Width = 0;
                            templateField.HeaderStyle.Width = 0;
                            gridView.Columns.Add(templateField);
                        }
                    }
                    else
                    {
                        BoundField _BoundField = new BoundField();
                        _BoundField.HeaderText = headerText;
                        _BoundField.DataField = dataField;
                        if (fieldWidth != "")
                        {
                            _BoundField.ItemStyle.Width = new Unit(double.Parse(fieldWidth));
                        }
                        if (formatString != "")
                        {
                            _BoundField.DataFormatString = formatString;
                        }
                        gridView.Columns.Add(_BoundField);
                    }
                }
            }

            DataRow rowMain = dynamicModel.DynamicMain.Rows[0];
            string divHeiht =rowMain.GetFieldValue("DIVHEIGTH");
            string divWeigth = rowMain.GetFieldValue("DIVWIDTH");
            string divGridHeiht = rowMain.GetFieldValue("GRIDVIEWHEIGTH");
            string divGridWeigth = rowMain.GetFieldValue("GRIDVIEWWIDTH");
            AspNetPager.PageSize = int.Parse(rowMain.GetFieldValue("PAGESIZE", "10"));
            if (divHeiht != "")
            {
                pnlGrid.Height = new Unit(double.Parse(divHeiht));
            }
            if (divWeigth == "")
            {
            }
            if (divGridHeiht != "")
            {
                gridView.Height = new Unit(double.Parse(divGridHeiht));
            }
            if (divGridWeigth != "")
            {
                gridView.Width = new Unit(double.Parse(divGridWeigth));
            }
        }
    }

    public class DynamicModel
    {
        public DataTable DynamicMain { get; set; }
        public DataTable DynamicDetail { get; set; }
        public string DynamicSql { get; set; }
        public string Sort { get; set; }

        public string PageID { get; set; }
        public string GridViewID { get; set; }
        public string CustomerID { get; set; }
    }
}
